/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.legendshop.oa.constants.FunctionEnum;
import com.legendshop.oa.model.ProjectMember;
import com.legendshop.oa.service.ProjectMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.legendshop.dao.support.PageSupport;
import com.legendshop.framework.controller.BaseController;
import com.legendshop.oa.constants.PathResolver;
import com.legendshop.oa.exception.BusinessException;
import com.legendshop.oa.model.Project;
import com.legendshop.oa.model.ProjectModule;
import com.legendshop.oa.pages.AdminTilesPage;
import com.legendshop.oa.pages.BackPage;
import com.legendshop.oa.pages.RedirectPage;
import com.legendshop.oa.security.UserDetail;
import com.legendshop.oa.security.UserManager;
import com.legendshop.oa.service.ProjectModuleService;
import com.legendshop.oa.service.ProjectService;
import com.legendshop.oa.util.PageSupportHelper;
import com.legendshop.util.AppUtils;

/**
 * The Class ProjectModuleController
 * 控制器
 */
@Controller
@RequestMapping("/admin/projectModule")
public class ProjectModuleController extends BaseController{
	
    @Autowired
    private ProjectModuleService projectModuleService;
    
    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectMemberService projectMemberService;

	/**
	 * 列表查询项目内部的模块
	 */
    @RequestMapping("/query/{projectId}")
    public String query(HttpServletRequest request, HttpServletResponse response, @PathVariable Long projectId,String name,String curPage) {
        UserDetail userDetail = UserManager.getUser(request);
        Project project = projectService.getProject(userDetail,projectId);
        
        PageSupport<ProjectModule> ps =  projectModuleService.getProjectModuleByProject(project.getId(),name,curPage);
        PageSupportHelper.savePage(request, ps);

        request.setAttribute("project", project);
        request.setAttribute("name", name);
        return PathResolver.getPath(request, response,  AdminTilesPage.PROJECT_MODULE_LIST_PAGE);
    }

	/**
	 * 保存项目模块
	 */
    @RequestMapping(value = "/save")
    @ResponseBody
    public String save(HttpServletRequest request, HttpServletResponse response, ProjectModule projectModule) {
    	
    	if(AppUtils.isBlank(projectModule.getProjectId())) {
    		throw new BusinessException("project id can not be null");
    	}
    	
    	if(AppUtils.isBlank(projectModule.getName())) {
    		throw new BusinessException("project name can not be null");
    	}

        UserDetail ud = UserManager.getUser(request);

        //检查是否在项目里
        boolean isInProject = projectMemberService.isMemberInProject(ud.getUsername(),projectModule.getProjectId());
        if(!ud.hasFunction(FunctionEnum.FUNCTION_VIEW_ALL_DATA) && !isInProject){
            //不在項目里不能填写工时
            return "fail";// 失败
        }

    	projectModule.setCreateTime(new Date());
        projectModuleService.saveProjectModule(projectModule);
        return "success";
    }

    /**
     * 批量保存项目模块
     */
    @RequestMapping(value = "/batchSave")
    @ResponseBody
    public String batchSave(HttpServletRequest request, HttpServletResponse response, Long projectId,  String[] name) {

        if(AppUtils.isBlank(projectId)) {
            throw new BusinessException("project id can not be null");
        }

        UserDetail ud = UserManager.getUser(request);

        //检查是否在项目里
        boolean isInProject = projectMemberService.isMemberInProject(ud.getUsername(),projectId);
        if(!ud.hasFunction(FunctionEnum.FUNCTION_VIEW_ALL_DATA) && !isInProject){
            //不在項目里不能填写工时
            return "fail";// 失败
        }

        if(name != null ){
            List<ProjectModule> list = new ArrayList<>();
            Date date = new Date();
            for (int i = 0; i < name.length; i++) {
                String name1 = name[i];
                if(AppUtils.isNotBlank(name1)){
                    ProjectModule projectMember = new ProjectModule();
                    projectMember.setProjectId(projectId);
                    projectMember.setCreateTime(date);
                    projectMember.setName(name1);
                    list.add(projectMember);
                }

            }

            projectModuleService.saveProjectModule(projectId, list);
        }

        return "success";
    }

	/**
	 * 删除
	 */
    @RequestMapping(value = "/delete/{id}")
    public String delete(HttpServletRequest request, HttpServletResponse response, @PathVariable
    Long id) {
        ProjectModule projectModule = projectModuleService.getProjectModule(id);
        if(projectModule != null) {

            UserDetail ud = UserManager.getUser(request);
            //检查是否在项目里
            boolean isInProject = projectMemberService.isMemberInProject(ud.getUsername(),projectModule.getProjectId());
            if(!ud.hasFunction(FunctionEnum.FUNCTION_VIEW_ALL_DATA) && !isInProject){
                //不在項目里不能填写工时
                String actionUrl  = RedirectPage.PROJECT_MODULE_LIST_QUERY.getNativeValue() + "/" + projectModule.getProjectId();
                return PathResolver.getPath(request, response,actionUrl, RedirectPage.VARIABLE);
            }


            projectModuleService.deleteProjectModule(projectModule);
            saveMessage(request, ResourceBundle.getBundle("i18n/ApplicationResources").getString("entity.deleted"));
            String actionUrl  = RedirectPage.PROJECT_MODULE_LIST_QUERY.getNativeValue() + "/" + projectModule.getProjectId();
            return PathResolver.getPath(request, response,actionUrl, RedirectPage.VARIABLE);
        }
        return null;
    }


   /**
	 * 加载编辑页面
	 */
	@RequestMapping(value = "/loadByProject/{projectId}")
	public String loadByPro(HttpServletRequest request, HttpServletResponse response, @PathVariable Long projectId) {
		request.setAttribute("projectId", projectId);
		return PathResolver.getPath(request, response, BackPage.PROJECT_MODULE_EDIT_PAGE);
	}
	
	/**
	 * 根据projectModuleId加载
	 */
    @RequestMapping(value = "/load/{projectModuleId}")
    public String load(HttpServletRequest request, HttpServletResponse response, @PathVariable Long projectModuleId) {
        ProjectModule projectModule = projectModuleService.getProjectModule(projectModuleId);
        request.setAttribute("projectModule", projectModule);
        request.setAttribute("projectId", projectModule.getProjectId());
        return PathResolver.getPath(request, response, BackPage.PROJECT_MODULE_EDIT_PAGE);
    }

    /**
     * 加载编辑页面
     */
    @RequestMapping(value = "/loadBatchByProject/{projectId}")
    public String loadBatchByProject(HttpServletRequest request, HttpServletResponse response, @PathVariable Long projectId) {
        request.setAttribute("projectId", projectId);
        List<ProjectModule> projectModules =  projectModuleService.getProjectModuleByProject(projectId);

        request.setAttribute("projectModules", projectModules);

        Integer count = projectModules == null ? 0 : projectModules.size();

        request.setAttribute("count", count);

        return PathResolver.getPath(request, response, BackPage.PROJECT_MODULE_BATCH_EDIT_PAGE);
    }


}
